package com.yuyou.tas.report.service.impl;

import com.yuyou.tas.report.mapper.ExamModelWordMapper;
import com.yuyou.tas.report.service.IExamModelWordPassService;
import com.yuyou.tas.report.service.IExamModelWordService;
import com.yuyou.tas.report.vo.req.ExamModelWordReq;
import com.yuyou.tas.report.vo.resp.ExamModelWordResp;
import com.yuyou.tas.service.api.report.entity.ExamModelWord;
import com.yuyou.tas.service.api.report.entity.ExamModelWordPass;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.tool.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class ExamModelWordServiceImpl implements IExamModelWordService{

	@Autowired
	private ExamModelWordMapper examModelWordMapper;

	@Autowired
	private IExamModelWordPassService examModelWordPassService;
	
	@Override
	public int deleteByPrimaryKey(Long id) {
		return examModelWordMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(ExamModelWord record) {
		record.setId(Sequence.getSequence().nextId());
		record.setCreateTime(new Date());
		record.setUpdateTime(new Date());
		return examModelWordMapper.insert(record);
	}

	@Override
	public ExamModelWord selectByPrimaryKey(Long id) {
		return examModelWordMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(ExamModelWord record) {
		record.setUpdateTime(new Date());
		return examModelWordMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public ResBoolSimpleInfo<String> add(ExamModelWord record) {
		this.insert(record);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	@Transactional
	public void addExamModelWord(ExamModelWordReq examModelWordReq, LoginRedisObj user) {
		ExamModelWord examModelWord = new ExamModelWord();
		examModelWord.setAuthOrganizationId(user.getOrganizationId());
		examModelWord.setCreateUser(user.getUserId());
		examModelWord.setName(examModelWordReq.getName());
		examModelWord.setRemark(examModelWordReq.getRemark());
		examModelWord.setIsUse(1);
		this.insert(examModelWord);

		examModelWordReq.getList().forEach(examModelWordPassReq -> {
			ExamModelWordPass examModelWordPass = new ExamModelWordPass();
			examModelWordPass.setAuthOrganizationId(user.getOrganizationId());
			examModelWordPass.setExamModelWordId(examModelWord.getId());
			examModelWordPass.setPassDescribe(examModelWordPassReq.getPassDescribe());
			examModelWordPass.setPassRemark(examModelWordPassReq.getPassRemark());
			examModelWordPass.setMaxInterval(examModelWordPassReq.getMaxInterval().setScale(2 , BigDecimal.ROUND_HALF_UP));
			examModelWordPass.setMinInterval(examModelWordPassReq.getMinInterval().setScale(2 , BigDecimal.ROUND_HALF_UP));

			examModelWordPassService.insert(examModelWordPass);
		});
	}

	@Override
	@Transactional
	public void updateExamModelWord(ExamModelWordReq examModelWordReq, LoginRedisObj user) {
		ExamModelWord examModelWord = selectByPrimaryKey(examModelWordReq.getId());
		if(examModelWord != null){
			examModelWord = new ExamModelWord();
			examModelWord.setId(examModelWordReq.getId());
			examModelWord.setName(examModelWordReq.getName());
			examModelWord.setRemark(examModelWordReq.getRemark());
			examModelWord.setUpdateUser(user.getUserId());
			updateByPrimaryKeySelective(examModelWord);

			//删除原来的区间
			examModelWordPassService.delByWordId(examModelWordReq.getId());
			//重新插入
			examModelWordReq.getList().forEach(examModelWordPassReq -> {
				ExamModelWordPass examModelWordPass = new ExamModelWordPass();
				examModelWordPass.setAuthOrganizationId(user.getOrganizationId());
				examModelWordPass.setExamModelWordId(examModelWordReq.getId());
				examModelWordPass.setPassDescribe(examModelWordPassReq.getPassDescribe());
				examModelWordPass.setPassRemark(examModelWordPassReq.getPassRemark());
				examModelWordPass.setMaxInterval(examModelWordPassReq.getMaxInterval().setScale(2 , BigDecimal.ROUND_HALF_UP));
				examModelWordPass.setMinInterval(examModelWordPassReq.getMinInterval().setScale(2 , BigDecimal.ROUND_HALF_UP));

				examModelWordPassService.insert(examModelWordPass);
			});

		}
	}

	@Override
	public void changeExamModelWordStatus(Long id, Integer isUse , Long userId) {
        ExamModelWord examModelWord = new ExamModelWord();
        examModelWord.setId(id);
        examModelWord.setIsUse(isUse);
        examModelWord.setUpdateUser(userId);
        updateByPrimaryKeySelective(examModelWord);
	}

	@Override
	public Integer selectExamModelWordsCount(Pagination<ExamModelWordResp> queryPgn) {
		return examModelWordMapper.selectExamModelWordsCount(queryPgn);
	}

	@Override
	public List<ExamModelWordResp> selectExamModelWords(Pagination<ExamModelWordResp> queryPgn) {
		return examModelWordMapper.selectExamModelWords(queryPgn);
	}

	@Override
	public ExamModelWordResp selectExamModelWordById(Long id) {
		return examModelWordMapper.selectExamModelWordById(id);
	}


}